/*cd C:\Business\Stata\4.17Multi-period-DID
import excel thesis.xlsx,first clear
rename city city
gen treat=0
replace treat=1 if city=="Shanghai"|city=="Tianjin"|city=="Fujian"|city=="Guangdong"|city=="Liaoning"|city=="Zhejiang"|city=="Henan"|city=="Hubei"|city=="Chongqing"|city=="Sichuan"|city=="Shanxi"|city=="Hainan"
*/

cd C:\Business\Stata\4.17Multi-period-DID
import excel 2.xlsx,first clear
gen time=0
replace time=1 if YEAR-starttime>=0&treat==1
//winsor2 TL time infre lngov trade ser indus ,replace cuts(1 99)/*缩尾*/
gen event = YEAR- starttime if treat==1
tab event, gen(eventt)
forvalues i = 1/17{
 replace eventt`i' = 0 if eventt`i' == .
}
drop eventt1
egen code=group(AREA)
xtset code YEAR
//reg TL time infre lngov trade ser indus eventt* ,r
xtreg TS time  infre lngov trade ser indus eventt* i.YEAR ,r
coefplot, ///
   keep(eventt*)  ///
   coeflabels(eventt1 = "-10"   ///
   eventt2 = "-9"              ///
   eventt3 = "-8"              ///
   eventt4 = "-7"               ///
   eventt5  = "-6"              ///
   eventt6  = "-5"              ///
   eventt7  = "-4"              ///
   eventt8  = "-3"              ///
   eventt9 = "-2"             ///
   eventt10 = "-1"               ///
   eventt11  = "0"              ///
   eventt12 = "1"              ///
   eventt13 = "2"              ///
   eventt14 = "3"              ///
   eventt15 = "4"              ///
   eventt16 = "5"               ///
   eventt17 = "6"  )            ///
   vertical                    ///
   yline(0)                    ///
   ytitle("Coef")              ///
   xtitle("Time passage relative to year of adoption of implied contract exception") ///
   addplot(line @b @at)        ///
   ciopts(recast(rcap))       
           ///
//coefplot,keep( eventt*) vertical recast(connect) yline(0)


save 3.dta,replace

use 3.dta,clear
//Descriptive statistics
winsor2 TS TL time infre lngov trade ser indus ,replace cuts(1 99)/*winsor2*/
outreg2 using ts Descriptive statistics, word replace sum(log) sortvar(TS TL time infre lngov trade ser indus) keep(TS TL time infre lngov trade ser indus)

//Correlate
logout,save(Correlate) word replace:pwcorr_a TS TL time infre lngov trade ser indus, star1(0.01) star5(0.05) star10(0.1)

//Regression
use 3.dta,clear
winsor2 TS time infre lngov trade ser indus ,replace cuts(1 99)/*winsor2*/
reg TS time  infre lngov trade ser indus 

est sto r1
outreg2 [r1 ] using TS Regression,word replace tstat bdec(4) tdec(2) sortvar(TS time infre lngov trade ser indus) keep(TS time infre lngov trade ser indus)


//Robustness test
use 3.dta,clear

gen fta_b=0
replace fta_b=1 if YEAR-starttime>=-4&starttime!=.
xtreg TS fta_b infre lngov trade ser indus i.YEAR,fe r
est sto r1
outreg2 [r1 ] using TS Robustness,word replace tstat bdec(4) tdec(2) sortvar(TS fta_b infre lngov trade ser indus) keep(TS fta_b infre lngov trade ser indus)

